package controler.events;

import java.util.ArrayList;
import java.util.List;

public class EventGenerator {

    private final List<EventsListener> listeners;
    private static EventGenerator instance = new EventGenerator();  //singleton

    public EventGenerator() {
        listeners = new ArrayList<>();
    }

    //singleton
    public static EventGenerator getInstance() {
        return instance;
    }

    public synchronized void addListener(EventsListener listener) {
        if (listener != null && !listeners.contains(listener)) {
            listeners.add(listener);
        }
    }

    public void removeListener(EventsListener listener) {
        listeners.remove(listener);
    }

    public void fireEvent(MyEvent myEvent) {
        synchronized (listeners) {
            for (EventsListener listener : listeners) {
                listener.eventHappened(myEvent);
            }
        }
    }
}
